package com.gph.saviorframework.mail.service.impl;

import com.gph.saviorframework.mail.model.SendMessage;
import com.gph.saviorframework.mail.service.MailSendService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMailMessage;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.stereotype.Component;

import javax.mail.BodyPart;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Transport;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Properties;

/**
 * Created by root on 16-8-15.
 */
@Component
public class MailSendServiceImpl implements MailSendService {

    @Autowired
    private JavaMailSender mailSender;

    /**
     * 发送简单消息
     *
     * @param textMessage
     * @return
     */
    public boolean sendTextMessage(String textMessage) {
        return false;
    }

    /**
     * 发送简单消息
     *
     * @param textMessages
     * @param tos          收件人列表
     * @return
     */
    public boolean sendTextMessage(String textMessages, String[] tos) {
        try{
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setSubject("default");
            simpleMailMessage.setText(textMessages);
            simpleMailMessage.setTo(tos);
            simpleMailMessage.setSentDate(new Date());
            mailSender.send(simpleMailMessage);
            return true;
        }
        catch (Exception ex){
            ex.printStackTrace();
        }

        return false;
    }

    /**
     * 发送简单消息
     *
     * @param subject
     * @param textMessages
     * @param tos          收件人列表
     * @return
     */
    public boolean sendTextMessage(String subject, String textMessages, String[] tos) {
        try{
            SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
            simpleMailMessage.setSubject(subject);
            simpleMailMessage.setText(textMessages);
            simpleMailMessage.setTo(tos);
            simpleMailMessage.setSentDate(new Date());
            simpleMailMessage.setFrom("admin01@qd.iscas.ac.cn");
            mailSender.send(simpleMailMessage);
            return true;
        }
        catch (Exception ex){
            ex.printStackTrace();
        }

        return false;
    }

    /**
     * 发送html信息
     *
     * @param htmlMessage
     * @return
     */
    public boolean sendHtmlMessage(String htmlMessage) {
        return false;
    }


    public boolean sendHtmlMessage(String htmlMessage, String subject, String[] receivers) {
        MimeMessage mimeMailMessage=mailSender.createMimeMessage();
        Properties properties=new Properties();
        InputStream inputStream=getClass().getResourceAsStream("/mail.properties");
        try {
            properties.load(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        try {
            MimeMessageHelper helper=new MimeMessageHelper(mimeMailMessage,true,"UTF-8");
            helper.setFrom(properties.getProperty("mail.smtp.from"));
            helper.setTo(receivers);
            helper.setSubject(subject);
            helper.setText(htmlMessage,true);

            mailSender.send(mimeMailMessage);
            return true;
        } catch (MessagingException e) {
            e.printStackTrace();
        }

        return false;
    }
}
